//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.salesperson.commission;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import org.jpgolay.me.order.OrderRow;
import org.jpgolay.me.persistence.OLD_RequestDB;
import org.jpgolay.me.util.MEDate;

public class OLD_CalculateCommission {

    public OLD_CalculateCommission() {
        resultat = false;
        continu = false;
        dateDebut = "";
        dateFin = "";
        commercialId = "";
        userId = "";
        familleArticleId = "";
        soldeLe = "";
        dbr = new OLD_RequestDB();
        dbr2 = new OLD_RequestDB();
    }

    public boolean calcule(int type) {
        calculeComm(type);
        return resultat;
    }

    public Vector getResultat() {
        return comm;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public void setDateDebut(String dateDebut) {
        this.dateDebut = dateDebut;
    }

    public void setDateFin(String dateFin) {
        this.dateFin = dateFin;
    }

    public void setCommercialId(String commercialId) {
        this.commercialId = commercialId;
    }

    private void calculeComm(int i) {
    }

    private BigDecimal getPrixRevient(String reference) {
        BigDecimal r = new BigDecimal(0);
        String requete2 = "select prix_revient, familles_articles_id from articles where ref_code_barre='" + reference + "'";
        dbr2.open();
        try {
            jeuResultats2 = dbr2.getTable(requete2);
            if (jeuResultats2.next()) {
                r = new BigDecimal(jeuResultats2.getString(1));
                familleArticleId = jeuResultats2.getString(2);
            }
        } catch (SQLException sqlex) {
            sqlex.printStackTrace();
            continu = false;
        }
        dbr2.close();
        return r;
    }

    private String getCoeffFamArt(String articleId) {
        String resultat = "";
        String date = "";
        MEDate dt = new MEDate();
        date = dt.toMysql(soldeLe);
        String requete2 = "select t from comm_par_article where articles_id ='" + articleId + "' and commerciaux_id = '" + commercialId + "' and debut <= '" + date + "' and fin >= '" + date + "'";
        String requete3 = "select t from comm_par_famille where famille_articles_id ='" + familleArticleId + "' and commerciaux_id = '" + commercialId + "' and debut <= '" + date + "' and fin >= '" + date + "'";
        dbr2.open();
        try {
            jeuResultats2 = dbr2.getTable(requete2);
            if (jeuResultats2.next()) {
                resultat = jeuResultats2.getString(1);
            } else {
                jeuResultats2 = dbr2.getTable(requete3);
                if (jeuResultats2.next()) {
                    resultat = jeuResultats2.getString(1);
                }
            }
        } catch (SQLException sqlex) {
            sqlex.printStackTrace();
            continu = false;
        }
        dbr2.close();
        return resultat;
    }
    private OLD_RequestDB dbr;
    private OLD_RequestDB dbr2;
    private ResultSet jeuResultats;
    private ResultSet jeuResultats2;
    private OrderRow cd;
    private Vector comm;
    private boolean resultat;
    private boolean continu;
    private String dateDebut;
    private String dateFin;
    private String commercialId;
    private String userId;
    private String familleArticleId;
    private String soldeLe;
    private final int VENTES_PERSONNEL = 0;
    private final int VENTES_GLOBAL = 1;
}
